package com.infoland.afanmall.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.my.entity.UserAccessEntity;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import okhttp3.Call;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    public static IWXAPI wxapi;
    private static final int RETURN_MSG_TYPE_LOGIN = 1;
    private static final int RETURN_MSG_TYPE_SHARE = 2;
    UserAccessEntity userLoginEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //如果没回调onResp，八成是这句没有写
        wxapi.handleIntent(this.getIntent(), this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        LogKit.E("  BaseReq ", "  ..............." + req);
    }

    @Override
    public void onResp(BaseResp resp) {
//        LogKit.E("baseResp:", JSONObject.toJSONString(resp));
        switch (resp.errCode) {
//            发送成功
            case BaseResp.ErrCode.ERR_OK:
                LogKit.E("发送成功 ---->   ", "  成功接收微信回调");
                CodeEntity codeEntity = JSONObject.parseObject(JSONObject.toJSONString(resp), CodeEntity.class);
                LogKit.E("成功接收微信回调 ---->   ", "    " + codeEntity);
                getAccessToken(codeEntity);
                break;
//            发送取消
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                loginToErro();
                finish();
                break;
//            发送被拒绝
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                loginToErro();
                finish();
                break;
//            发送返回
            default:
                loginToErro();
                finish();
                break;
        }

    }

    /***获取AccessToken信息**/
    private void getAccessToken(CodeEntity codeEntity) {
        LogKit.E("准备获取 AccessToken---->   ", "  准备获取 AccessToken");
        JSONObject para = new JSONObject();
        para.put("code", codeEntity.getCode());
        para.put("type", 2);
        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("微信登录  200--->", "  " + response200);
                    userLoginEntity = JSONObject.parseObject(response200, UserAccessEntity.class);
                    /***开始登录*/
                    if (userLoginEntity.isSuccess()) {
                        LogKit.E("response200 ---->   ", "  开始登录");
                        loginToYes();
                        finish();
                    } else {
                        loginToErro();
                    }
                },
                success401 -> {
                    LogKit.E("微信登录  success401--->", "  " + success401);
                    loginToErro();
                },
                error -> {
                    LogKit.E("微信登录  error--->", "  " + error);
                    loginToErro();
                }
        ).post(CONST.WEI_XIN_LOGIN, para, this);
    }

    /***修改登录状态--成功*/
    private void loginToYes() {
        UserKit.setUserCacheYes(userLoginEntity.getObject().getAccess_token(), userLoginEntity.getObject().getRefresh_token());
    }

    /***修改登录状态--错误*/
    private void loginToErro() {
        LogKit.E("loginToErro ---->   ", "  发送取消");
        UserKit.putIsLogin(false);
        finish();
        ToastUtils.showShort("登录失败");
    }

    @Override
    protected void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();
    }
}

